package com.hc.dao;

import com.hc.bean.ProductInfo;
import com.hc.dao.provider.ProductInfoProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface ProductInfoDao {
    /*模糊分页查询*/
    @SelectProvider(type = ProductInfoProvider.class,method = "selectProductInfoByParams")
    @Results({
            @Result(column = "tid",property = "tid"),
            @Result(column = "tid",property = "type",one = @One(select = "com.hc.dao.TypeDao.getTypeById"))
    })
    public List<ProductInfo> getProductList(Map<String,Object> params);

    /*模糊查询总记录数*/
    @SelectProvider(type = ProductInfoProvider.class,method = "getProductInfoCount")
    public Integer getProductInfoCount(Map<String,Object> params);

    /*添加商品信息*/
    @Insert("insert into product_info(code,name,tid,brand,pic,num,price,intro,status) " +
            "values(#{code},#{name},#{type.id},#{brand},#{pic},#{num},#{price},#{intro},#{status})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    public int addProductInfo(ProductInfo productInfo);

    /*修改商品信息*/
    @Update("<script> " +
            "update product_info " +
            "<set>"+
            "<if test='code!=null'>,code = #{code} </if> " +
            "<if test='name!=null'>,name = #{name} </if> " +
            "<if test='type.id!=null'>,tid = #{type.id} </if> " +
            "<if test='brand!=null'>,brand = #{brand} </if> " +
            "<if test='pic!=null'>,pic = #{pic} </if> " +
            "<if test='num!=null'>,num = #{num} </if> " +
            "<if test='price!=null'>,price = #{price} </if> " +
            "<if test='intro!=null'>,intro = #{intro} </if> " +
            "<if test='status!=null'>,status = #{status} </if> " +
            "where id = #{id} " +
            "</set>"+
            "</script>")
    public int updateProductInfo(ProductInfo productInfo);

    /*修改商品状态*/
    /*
    * @Param主要应用在Dao层
    * 注解中的sql语句有多个条件参数，且和方法中的参数名称不一致，此时可以使用@Param注解
    * 传递多个参数是需要加上,否则会提示 Required String parameter 'xxx' is not present
    * #{}相当于占位符,${}可以实现sql注入,操作表,
    * 这里的in (${ids}) 是sql注入,所以要用${}
    * */
    @Update("update product_info set status=#{status} where id in (${ids})")
    public int changeProductStatus(@Param("status") Integer status,@Param("ids") String ids);

    /*根据商品id获取商品信息*/
    @Select("select * from product_info where id=#{pid}")
    public ProductInfo getProductInfoByPid(int pid);

    /*获取所有在手商品信息*/
    @Select("select * from product_info where status=1")
    public List<ProductInfo> getOnSaleProduct();

    /*根据商品id获取商品的价格*/
    @Select("select price from product_info where id=#{pid}")
    public double getPriceById(int pid);

}
